package com.controller.admin;

import com.bean.vo.admin.AllAdminsVO;
import com.bean.vo.admin.Result;
import com.bean.vo.admin.AdminLoginSuccessVO;
import com.bean.vo.user.AllUserVO;
import com.google.gson.Gson;
import com.service.admin.AdminServiceImplement;
import com.utils.GsonUtils;
import com.utils.StringUtils;
import org.apache.commons.io.IOUtils;
import com.bean.bo.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.temporal.ValueRange;
import java.util.Date;
import java.util.List;

@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {
    private final static Gson gson = GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/admin/", "");
       if ("allAdmins".equals(action)){
           showAllAdmins(request,response);
       }
       if ("deleteAdmins".equals(action)){
           deleteAdmin(request,response);
       }
       if ("getAdminsInfo".equals(action)){
           showUpdateAdmin(request,response);
       }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/admin/", "");
        if ("login".equals(action)){
            login(request,response);
        }
        if ("addAdminss".equals(action)){
            addAdmin(request,response);
        }
        if ("updateAdminss".equals(action)){
            doUpdateAdmin(request,response);
        }
        if("getSearchAdmins".equals(action)){
            selectByconditions(request,response);
        }
        if ("changePwd".equals(action)){
            changePassword(request,response);
        }
        if ("logoutAdmin".equals(action)){
            logOut(request,response);
        }
    }

    private void logOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        response.getWriter().println(gson.toJson(new Result(0,null,null)));
    }

    /**
 * @Author xiao-fan
 * @Description  修改密码
 * @Date 22:14 2021/6/16
 **/
    private void changePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream);
        ChangePasswordAdmin changePasswordAdmin = gson.fromJson(requestString, ChangePasswordAdmin.class);
        if (StringUtils.idEmpty(changePasswordAdmin.getNewPwd())&&StringUtils.idEmpty(changePasswordAdmin.getOldPwd())){
            Result result = new Result(10000, null, "旧密码错误");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        if (!changePasswordAdmin.getConfirmPwd().equals(changePasswordAdmin.getNewPwd())){
            Result result = new Result(10000, null, "请保持确认新密码一致");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        int changePasswordState = adminServiceImplement.changePassword(changePasswordAdmin);
        if (changePasswordState==200){
            Result result = new Result(0, null, null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        Result result = new Result(10000, null, "数据库修改异常");
        response.getWriter().println(gson.toJson(result));


    }

    /**
 * @Author xiao-fan
 * @Description  按条件查询
 * @Date 21:27 2021/6/16
 **/
    private void selectByconditions(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        SelectAdmin selectAdmin = gson.fromJson(requestString, SelectAdmin.class);
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        List<AllAdminsVO> allAdminsVOSList = adminServiceImplement.selectByConditions(selectAdmin);
        Result result = new Result(0, allAdminsVOSList, null);
        response.getWriter().println(gson.toJson(result));

    }

    /**
 * @Author xiao-fan
 * @Description  修改操作
 * @Date 21:26 2021/6/16
 **/
    private void doUpdateAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String s = IOUtils.toString(inputStream);
        UpdateAdmin updateAdmin = gson.fromJson(s, UpdateAdmin.class);
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        int i = adminServiceImplement.updateAdmin(updateAdmin);
        if (i==200){
            Result result = new Result(0, null, null);
            response.getWriter().println(gson.toJson(result));
            return;
        }

    }

    /**
     * @Author xiao-fan
     * @Description  展示要修改的管理员详细信息
     * @Date 20:39 2021/6/16
     **/
    private void showUpdateAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        Integer id = Integer.parseInt(idString);
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        UpdateAdmin updateAdmin = adminServiceImplement.selectUpdateAdmin(id);
        Result result = new Result(0, updateAdmin, null);
        response.getWriter().println(gson.toJson(result));
    }
    /**
     * @Author xiao-fan
     * @Description  新增管理员
     * @Date 20:10 2021/6/16
     **/
    private void addAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String s = IOUtils.toString(inputStream);
        AdminAdd adminAdd = gson.fromJson(s, AdminAdd.class);
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        int addState = adminServiceImplement.addAdmin(adminAdd);
        if (addState==200){
            Result result = new Result(0, null, null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        Result result = new Result(10000, null, "该账号不允许重复使用");
        response.getWriter().println(gson.toJson(result));

    }
/**
 * @Author xiao-fan
 * @Description 删除操作
 * @Date 19:15 2021/6/16
 **/
    private void deleteAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        Integer id=Integer.parseInt(idString);
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        int deleteState = adminServiceImplement.deleteAdmin(id);
        if(deleteState==200){
            Result result = new Result(0, null, null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
    }
/**
 * @Author xiao-fan
 * @Description  获取所有管理员用户
 * @Date 19:15 2021/6/16
 **/
    private void showAllAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        List<AllAdminsVO> allAdminsVOS = adminServiceImplement.showAllAdmins();
        Result result = new Result(0, allAdminsVOS, null);
        response.getWriter().println(gson.toJson(result));
    }


/**
 * @Author xiao-fan
 * @Description  登录的具体实现方法
 * @Date 20:04 2021/6/16
 **/
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestJson = IOUtils.toString(inputStream);
        AdminLogin adminLogin = gson.fromJson(requestJson, AdminLogin.class);
        if (StringUtils.idEmpty(adminLogin.getEmail())||StringUtils.idEmpty(adminLogin.getPwd())){
            Result result = new Result(10000, null, "用户名、密码不能为空");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        AdminServiceImplement adminServiceImplement = new AdminServiceImplement();
        int login = adminServiceImplement.login(adminLogin);
        if (login==200){
            HttpSession session = request.getSession();
            session.setAttribute("username",adminLogin.getEmail());
            AdminLoginSuccessVO adminLoginSuccessVO = new AdminLoginSuccessVO(adminLogin.getEmail(),adminLogin.getPwd());
            Result result = new Result(0, adminLoginSuccessVO, null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        else {
            response.getWriter().println(gson.toJson(new Result(10000,null,"密码不正确")));
        }
    }
}
